package com.ows.powercontrol.service.impl;

import com.ows.powercontrol.pojo.Status;
import com.ows.powercontrol.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.net.Socket;

/**
 * @author cq
 * @date 2020/6/22    @Value("${port}")
 *     private int port ;
 *     @Value("${host}")
 *     private String host; 19:52
 */
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    public static Socket socket;
    public static String str = "0-0-0-0-2";
    @Value("${host}")
    private String host;
    @Value("${port}")
    private  int port;
    @Override
    public void quicken() {
        checkSocket();
        send("3");
    }

    public void send(String msg) {
//        checkSocket();
        try {
            Writer writer = new OutputStreamWriter(socket.getOutputStream());
            char[] chars = msg.toCharArray();
            writer.write(chars);
            writer.flush();

        } catch (IOException e) {
            try {
                socket = new Socket(host, port);
                Writer writer = new OutputStreamWriter(socket.getOutputStream());
                char[] chars = msg.toCharArray();
                writer.write(chars);
                writer.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    @Override
    public Status getStatus() {
        try {
            String[] split = str.split("-");
            Status status = new Status();
            status.setHz(split[0]);
            //处理电流过大不真实相差100倍
          if (!split[1].equals("0")){
              double a=Double.parseDouble(split[1]);
              BigDecimal bigDianLiu = new BigDecimal(a);
              BigDecimal differenceLiu = new BigDecimal(0.01);
              String realA = bigDianLiu.multiply(differenceLiu).toString().substring(0,4);
              status.setA(realA);
              //处理电压过大不真实相差10倍
              double v=Double.parseDouble(split[2]);
              BigDecimal bigDianV = new BigDecimal(v);
              BigDecimal differenceV = new BigDecimal(0.1);
              String realV = bigDianV.multiply(differenceV).toString().substring(0,4);
              status.setV(realV);
          }else {
              status.setA("0");
              status.setV("0");
          }
          status.setDirection(split[4]);
            status.setSpeed(split[3]);
            return status;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void revers() {
        checkSocket();
        send("7");
    }

    @Override
    public void up() {
        checkSocket();
        send("1");
    }


    @Override
    public void slow() {
        checkSocket();
        send("4");
    }

    @Override
    public void stop() {
        checkSocket();
        send("2");
    }


    public void checkSocket() {
        boolean closed = socket.isClosed();
        if (closed) {
            try {
                socket = new Socket(host, port);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        BigDecimal bigDianLiu = new BigDecimal(219);
        BigDecimal difference = new BigDecimal(0.01);
        String real = bigDianLiu.multiply(difference).toString().substring(0,4);
        System.out.println(real);
    }
}
